﻿using Medora.DataContract;
using Medora.Model.Institutions;
using Medora.Services.Utils;

namespace Medora.Services.Institutions
{
    public class SpecialitiesService : ServiceBase<Speciality, SpecialityFilter>
    {
        public SpecialitiesService(IUow uow, IUserService userService, IFilterConstructor<SpecialityFilter, Speciality> filterConstructor) : base(uow, userService, filterConstructor)
        {
            _repository = _uow.Specialities;
        }
    }

    /* public class SpecialitiesService : IService<Speciality, SpecialityFilter>
    {
        private readonly IUow _uow;
        private readonly IUserService _userService;
        private readonly IFilterConstructor<SpecialityFilter, Speciality> _filterConstructor;

        public SpecialitiesService(IUow uow, IUserService userService, IFilterConstructor<SpecialityFilter, Speciality> filterConstructor)
        {
            _uow = uow;
            _userService = userService;
            _filterConstructor = filterConstructor;
        }

        public IPagedEntities<Speciality> GetPaged(int pageSize, int page, SpecialityFilter filter = null, SortInfo sortInfo = null)
        {
            
            Expression<Func<Speciality, bool>> startE = o => o.Deleted != true;
            var finalE = ConstructFilter(startE, filter);
            if (sortInfo == null || string.IsNullOrEmpty(sortInfo.Field))
            {
                sortInfo = new SortInfo { Direction = SortOrderType.Desc, Field = "CreatedOn" };
            }

            var entities = _uow.Specialities.GetTake(pageSize, page, finalE, sortInfo).ToList();
            
            
            return new PagedEntities<Speciality>
                {
                    Entities = entities,
                    Count = _uow.Specialities.GetCount(finalE)
                };
            
        }
        
        public bool Save(Guid userId, Speciality speciality)
        {
            //todo add logging on save
            try
            {
                if (speciality.Id != Guid.Empty)
                {
                    _uow.Specialities.Update(speciality);
                }
                else
                {
                    speciality.CreatedBy = userId;
                    speciality.CreatedOn = DateTime.Now;
                    
                    _uow.Specialities.Add(speciality);
                }
                _uow.Commit();
                return true;
            }
            catch (Exception e)
            {
                Logger.WriteLog(e);
                throw new Exception(Translator.Instance.Translate("Error during data save"));
            }
        }
        
        public bool Delete(List<Guid> ids)
        {
            try
            {
                var specialities = _uow.Specialities.Get(o => ids.Contains(o.Id)).ToList();
                foreach (var speciality in specialities)
                {
                    speciality.Deleted = true;
                    _uow.Specialities.OneFieldUpdate(speciality, d => d.Deleted);
                }
                _uow.Commit();
                return true;
            }
            catch (Exception e)
            {
                Logger.WriteLog(e);
                throw new Exception(Translator.Instance.Translate("Error during data save"));
            }
        }

        protected Expression<Func<Speciality, bool>> ConstructFilter(Expression<Func<Speciality, bool>> startE, SpecialityFilter filter)
        {
            if (filter == null) return startE;
            return _filterConstructor.GetFilter(filter, startE) ?? startE;
        }
    }*/
}